package com.lbc.service.impl;

import com.lbc.bean.dto.DonateProjectDTO;
import com.lbc.bean.dto.DonateProjectListBackDTO;
import com.lbc.bean.entity.DonateProject;
import com.lbc.bean.vo.DonateInfoVO;
import com.lbc.bean.vo.DonateProjectVO;
import com.lbc.common.utils.JwtUtils;
import com.lbc.bean.vo.DonateProjectDetailVO;
import com.lbc.dao.custom.CtDonateMapper;
import com.lbc.dao.custom.CtDonateProjectMapper;
import com.lbc.service.DonateProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xyl
 * @date 2021-03-18 16:25
 */
@Service
public class DonateProjectServiceImpl implements DonateProjectService {

    @Autowired
    private CtDonateProjectMapper donateProjectMapper;
    @Autowired
    private CtDonateMapper donateMapper;

    @Override
    public int add(DonateProjectDTO donateProjectDTO) {
        donateProjectDTO.setDonateProjectId(null);
        DonateProject donateProject = new DonateProject();
        BeanUtils.copyProperties(donateProjectDTO,donateProject);
        Date date = new Date();
        donateProject.setCreateTime(date);
        donateProject.setUpdateTime(date);
        donateProject.setCreateBy(JwtUtils.getUserId());
        donateProject.setCurrentMoney(0f);
        return donateProjectMapper.insert(donateProject);
    }

    @Override
    public int modify(DonateProjectDTO donateProjectDTO) {
        DonateProject donateProject = new DonateProject();
        BeanUtils.copyProperties(donateProjectDTO,donateProject);
        donateProject.setUpdateTime(new Date());
        donateProject.setUpdateBy(JwtUtils.getUserId());
        return donateProjectMapper.updateByPrimaryKeySelective(donateProject);
    }

    @Override
    public int remove(Integer donateProjectId) {
        return donateProjectMapper.deleteByPrimaryKey(donateProjectId);
    }

    @Override
    public List<DonateProjectVO> listVo(Integer state) {
        return donateProjectMapper.selectByState(state);
    }

    @Override
    public DonateProjectDetailVO getDetailVO(Integer donateProjectId) {
        DonateProjectDetailVO dpvo = new DonateProjectDetailVO();
        DonateProject donateProject = donateProjectMapper.selectByPrimaryKey(donateProjectId);
        BeanUtils.copyProperties(donateProject,dpvo);
        dpvo.setDonateCount(donateMapper.selectDonateCount(0,donateProjectId));
        dpvo.setMaxDonateMoney(donateMapper.selectMaxMoney(0,donateProjectId));
        dpvo.setDonateInfo(donateMapper.selectDonateInfoByProjectId(0,donateProjectId));
        dpvo.setCurrentMoney(donateMapper.selectDonateMoney(0,donateProjectId));
        dpvo.setState(donateProject.getDeadline().getTime() < new Date().getTime());
        return dpvo;
    }

    @Override
    public List<DonateProjectVO> listBackVo(DonateProjectListBackDTO dto) {
        return donateProjectMapper.selectBackList(dto);
    }
}